<?php

namespace App\Console\Commands;

use App\Http\Model\FinanceKk;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RepairKKdatas extends Command
{
    /**
     * 修复指定公会金币
     * 参数 --data='{json}'
     */
    protected $signature = 'RepairKKdatas';

    /**
     * 
     * The console command description.
     *
     * @var string
     */
    protected $description = '临时修复公会流水记录以及公会长金币';
    protected $connection = 'api'; 
    protected $financeModel;//金币结算模型

    protected $gift_stream_table = 'gift_stream';//礼物流水
    protected $finance_kk_stream_table = 'finance_kk_stream';//金币流水

    protected $log = 'dailyall_waterstream';//所有房间所有流水日志
    protected $errorLog = 'error_dailyall_waterstream';//所有房间所有流水错误日志
    

    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct(); 
        $this->financeModel =  new FinanceKk();
    }

    /**
     *
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
        try{
            DB::beginTransaction();
            $StartTime = '2023-09-12 01:16:00';
            $timestortTime = strtotime($StartTime);
            $timestortEndTime = '1694690865';
            //指定用户流水
            $userid = '22229';
            $union_userid = '27429';

            $gift_steam = DB::table($this->gift_stream_table)
            ->where('user_id',$userid)
            ->whereBetween('change_timestamp',[$timestortTime,$timestortEndTime])
            ->get();
            
            $gift_steam = $this->objToArray($gift_steam);
            if(empty($gift_steam)){
                die;
            }
            //公会房间流水
            foreach($gift_steam as $g){
                $kk_value = intval(($g['gift_price']*$g['gift_num'])*0.1); //公会结算0.1
                //给工会长用户加kk值
                $res = $this->financeModel->inc($union_userid,0,$g['room_id'],30,$kk_value,'公会奖励-实时结算-补发金币');
                echo "结算完成-".$g['stream_id'].PHP_EOL;
            }
            
            DB::commit();
        }catch (\Exception $e) {
            DB::rollBack();
            write_log($this->errorLog, "房间流水错误信息", '发送错误文件===>'.$e->getFile().PHP_EOL."发生错误行===>".$e->getLine().PHP_EOL.'发生错误信息====>'.$e->getMessage());
        }
    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }
}
